﻿#include "algo_cam_l_form.h"
#include "ui_algo_cam_l_form.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "singleton.h"

Algo_Cam_L_Form::Algo_Cam_L_Form(QString chnid, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Algo_Cam_L_Form)
{
    ui->setupUi(this);
    m_chnid = chnid;
    ui->led_chnid->setText(m_chnid);
    this->on_btn_getparam_1_clicked();
    this->on_btn_getparam_2_clicked();
    this->on_btn_getparam_3_clicked();
    this->InputValidator();


}

Algo_Cam_L_Form::~Algo_Cam_L_Form()
{
    delete ui;
}

void Algo_Cam_L_Form::InputValidator()
{
    QRegExp reg("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");
    ui->led_ipcip->setValidator(new QRegExpValidator(reg));
    ui->led_jilianip->setValidator(new QRegExpValidator(reg));
    ui->led_thresh->setValidator(new QRegExpValidator(QRegExp("^0\\.[1-9]{1,2}$")));
    ui->led_ipcuser->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
    ui->led_ipcpsw->setValidator(new QRegExpValidator(QRegExp("^[A-Za-z0-9]+$")));
    ui->led_modbusport->setValidator(new QRegExpValidator(QRegExp("^[0-9]*$")));
    ui->led_quyurenshu->setValidator(new QRegExpValidator(QRegExp("^[0-9]*$")));
    ui->led_maxdis->setValidator(new QRegExpValidator(QRegExp("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$")));
    ui->led_mindis->setValidator(new QRegExpValidator(QRegExp("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$")));

}

void Algo_Cam_L_Form::on_btn_getparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetAlgoParam", body);
}

void Algo_Cam_L_Form::on_btn_setparam_1_clicked()
{
    QJsonObject body;
    body.insert("chnId", ui->led_chnid->text());
    body.insert("enable", int(ui->sb_enable->getChecked()));
    body.insert("thresh", ui->led_thresh->text().toFloat());
    body.insert("url", ui->led_url->text());
    QStringList m_type = this->type();
    body.insert("eventType",  m_type.at(0));
    body.insert("alarmType", m_type.at(1));
    body.insert("alarmGrade", ui->cbx_alarmgrade->currentIndex());
    body.insert("ipcIp", ui->led_ipcip->text());
    body.insert("ipcUser", ui->led_ipcuser->text());
    body.insert("ipcPassword", ui->led_ipcpsw->text());
    Singleton::Instance()->onSendParam("SetAlgoParam", body);
}

void  Algo_Cam_L_Form::onGetAlgoParam(QJsonObject obj)
{
    ui->sb_enable->setChecked(obj.value("enable").toInt());
    ui->led_thresh->setText(QString::number(obj.value("thresh").toDouble(), 'f', 10));
    ui->led_url->setText(obj.value("url").toString());
    QString eventType = obj.value("eventType").toString();
    if(eventType == "XJ_000")   ui->cbx_eventtype->setCurrentIndex(0);
    else if(eventType == "PD_000")   ui->cbx_eventtype->setCurrentIndex(1);
    else if(eventType == "RQ_000")   ui->cbx_eventtype->setCurrentIndex(2);
    else if(eventType == "XW_000")   ui->cbx_eventtype->setCurrentIndex(3);
    else if(eventType == "ZC_000")   ui->cbx_eventtype->setCurrentIndex(4);
    else if(eventType == "JL_000")   ui->cbx_eventtype->setCurrentIndex(5);
    else if(eventType == "CY_000")   ui->cbx_eventtype->setCurrentIndex(6);
    else if(eventType == "HJ_000")   ui->cbx_eventtype->setCurrentIndex(7);
    else if(eventType == "SB_000")   ui->cbx_eventtype->setCurrentIndex(8);
    if(obj.value("alarmType").toString() != "")
    {
        if(eventType == "PD_000")
            ui->cbx_alarmtype->setCurrentIndex(obj.value("alarmType").toString().split("_").at(1).toInt() - 5);
        else
            ui->cbx_alarmtype->setCurrentIndex(obj.value("alarmType").toString().split("_").at(1).toInt() - 1);
    }
    ui->cbx_alarmgrade->setCurrentIndex(obj.value("alarmGrade").toInt());
    ui->led_ipcip->setText(obj.value("ipcIp").toString());
    ui->led_ipcuser->setText(obj.value("ipcUser").toString());
    ui->led_ipcpsw->setText(obj.value("ipcPassword").toString());
}

void Algo_Cam_L_Form::on_btn_getparam_2_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    Singleton::Instance()->onSendParam("GetExternalParam", body);
}

void Algo_Cam_L_Form::on_btn_setparam_2_clicked()
{
    QJsonObject body;
    body.insert("chnId", ui->led_chnid->text());
    body.insert("linkIoIn", int(ui->guanlianio->getChecked()));
    body.insert("linkPlc", int(ui->guanlianmodbus->getChecked()));
    body.insert("plcPort", ui->led_modbusport->text().toInt());
    body.insert("cascadeMode", ui->cbx_jilianio->currentIndex());
    body.insert("devIp", ui->led_jilianip->text());
    body.insert("modbusOut", int(ui->modbusalarm->getChecked()));
    Singleton::Instance()->onSendParam("SetExternalParam", body);
}

void  Algo_Cam_L_Form::onGetExternalParam(QJsonObject obj)
{
    ui->guanlianio->setChecked(obj.value("linkIoIn").toInt());
    ui->guanlianmodbus->setChecked(obj.value("linkPlc").toInt());
    ui->led_modbusport->setText(QString::number( obj.value("plcPort").toInt()));
    ui->cbx_jilianio->setCurrentIndex(obj.value("cascadeMode").toInt());
    ui->led_jilianip->setText(obj.value("devIp").toString());
    ui->modbusalarm->setChecked(obj.value("modbusOut").toInt());
}

void Algo_Cam_L_Form::on_btn_getparam_3_clicked()
{
    QJsonObject body;
    body.insert("chnId", m_chnid);
    if(ui->stackedWidget->currentWidget() == ui->page_renshu)
    {
        body.insert("zone", ui->cbx_renshu_quyu->currentIndex());
    }
    else if(ui->stackedWidget->currentWidget() == ui->page_quyu)
    {
        body.insert("zone", ui->cbx_quyu_quyu->currentIndex());
    }
    else if(ui->stackedWidget->currentWidget() == ui->page_ceju)
    {
        body.insert("zone", 0);
    }
    Singleton::Instance()->onSendParam("GetAlarmOut", body);
}

void Algo_Cam_L_Form::on_btn_setparam_3_clicked()
{
    QJsonObject body;
    body.insert("chnId", ui->led_chnid->text());
    if(ui->stackedWidget->currentWidget() == ui->page_renshu)
    {
        body.insert("zone", ui->cbx_renshu_quyu->currentIndex());
    }
    else if(ui->stackedWidget->currentWidget() == ui->page_quyu)
    {
        body.insert("zone", ui->cbx_quyu_quyu->currentIndex());
    }
    else if(ui->stackedWidget->currentWidget() == ui->page_ceju)
    {
        body.insert("zone", 0);
    }
    body.insert("enUpProtocol", int(ui->xieyishangbao->getChecked()));
    body.insert("enSpeaker", int(ui->yuyinbobao->getChecked()));
    QJsonObject ioObj;
    ioObj.insert("out_0", int(ui->IO_OUT1->isChecked()));
    ioObj.insert("out_1", int(ui->IO_OUT2->isChecked()));
    ioObj.insert("out_2", int(ui->IO_OUT3->isChecked()));
    ioObj.insert("out_3", int(ui->IO_OUT4->isChecked()));
    body.insert("ioOut", ioObj);
    body.insert("maxPeople", ui->led_quyurenshu->text().toInt());
    body.insert("distanceMin", ui->led_mindis->text().toFloat());
    body.insert("distanceMax", ui->led_maxdis->text().toFloat());
    Singleton::Instance()->onSendParam("SetAlarmOut", body);
}

void  Algo_Cam_L_Form::onGetAlarmOut(QJsonObject obj)
{
    ui->xieyishangbao->setChecked(obj.value("enUpProtocol").toInt());
    ui->yuyinbobao->setChecked(obj.value("enSpeaker").toInt());
    QJsonObject ioout = obj.value("ioOut").toObject();
    ui->IO_OUT1->setChecked( ioout.value("out_0").toInt());
    ui->IO_OUT2->setChecked( ioout.value("out_1").toInt());
    ui->IO_OUT3->setChecked( ioout.value("out_2").toInt());
    ui->IO_OUT4->setChecked( ioout.value("out_3").toInt());
    ui->led_quyurenshu->setText(obj.value("maxPeople").toString());
    ui->led_mindis->setText(obj.value("distanceMin").toString());
    ui->led_maxdis->setText(obj.value("distanceMax").toString());
}

void Algo_Cam_L_Form::on_cbx_eventtype_currentIndexChanged(int eventtype)
{
    QStringList list_alarm ;
    switch (eventtype)
    {
        case 0:
            list_alarm << "巡检轨迹" << "人脸识别";
            break;
        case 1:
            list_alarm  << "皮带跑偏" << "堆煤检测";
            break;
        case 2:
            list_alarm << "人员入侵" ;
            ui->stackedWidget->setCurrentWidget(ui->page_renshu);
            break;
        case 3:
            list_alarm << "安全帽检测" << "人员脱岗检测" << "吸烟检测" << "坐猴车" << "超挂车检测"
                       << "个人防护用品佩带" << "挂网检测" << "固定岗位工使用手机" << "乘车规范性检测"
                       << "设置遮拦检测" << "人员违规乘坐皮带" << "人员挂安全绳检测" << "信号灯识别";
            break;
        case 4:
            list_alarm << "钻杆计数" ;
            break;
        case 5:
            list_alarm << "距离监测报警" ;
            ui->stackedWidget->setCurrentWidget(ui->page_ceju);
            break;
        case 6:
            list_alarm << "划定区域内人员计数" << "公共区域内人数统计";
            break;
        case 7:
            list_alarm << "烟火识别" << "积水检测";
            break;
        case 8:
            list_alarm << "护帮支护检测" << "风门同时打开检测" << "人员进入自动开关风门"
                       << "阻车器复合检测" << "斜巷一坡三挡" << "单轨吊识别";
            break;
        default:
            break;
    }
    ui->cbx_alarmtype->clear();
    ui->cbx_alarmtype->addItems(list_alarm);
}

QStringList Algo_Cam_L_Form::type()
{
    QStringList type;
    QString eventtype;
    QString alarmtype;
    switch (ui->cbx_eventtype->currentIndex())
    {
        case 0:
            eventtype = "XJ_000";
            alarmtype = QString("XJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 1:
            eventtype = "PD_000";
            alarmtype = QString("PD_%1").arg(ui->cbx_alarmtype->currentIndex() + 5, 3, 10, QChar('0'));
            break;
        case 2:
            eventtype = "RQ_000";
            alarmtype = QString("RQ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 3:
            eventtype = "XW_000";
            alarmtype = QString("XW_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 4:
            eventtype = "ZC_000";
            alarmtype = QString("ZC_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 5:
            eventtype = "JL_000";
            alarmtype = QString("JL_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 6:
            eventtype = "CY_000";
            alarmtype = QString("CY_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 7:
            eventtype = "HJ_000";
            alarmtype = QString("HJ_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        case 8:
            eventtype = "SB_000";
            alarmtype = QString("SB_%1").arg(ui->cbx_alarmtype->currentIndex() + 1, 3, 10, QChar('0'));
            break;
        default:
            break;
    }
    type.append(eventtype);
    type.append(alarmtype);
    return  type;
}
